<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('用户新增', '', '')"/>
</head>
<body>
<form class="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <input id="depId" name="depId"  type="hidden"/>

                <div class="layui-form-item">
                    <label class="layui-form-label">用户账号<span style="color: red;">*</span></label>
                    <div class="layui-input-block">
                        <input type="text" name="username" autocomplete="off" placeholder="请输入用户账号" class="layui-input" lay-verify="required|username" required>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">用户密码<span style="color: red;">*</span></label>
                    <div class="layui-input-block">
                        <input type="password" name="newpassword" autocomplete="off" value="abc123456" placeholder="请输入密码" class="layui-input" lay-verify="required|password" required>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">确认密码<span style="color: red;">*</span></label>
                    <div class="layui-input-block">
                        <input type="password" name="password" autocomplete="off" value="abc123456" placeholder="请再次确认密码" class="layui-input" lay-verify="required|password" required>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">昵称<span style="color: red;">*</span></label>
                    <div class="layui-input-block">
                        <input type="text" name="nickname" autocomplete="off" placeholder="请输入昵称" class="layui-input" lay-verify="required|nickname" required>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">真实姓名</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" autocomplete="off" placeholder="请输入真实姓名" class="layui-input" lay-verify="name">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">性别:<span style="color: red;">*</span></label>
                    <div class="layui-input-block">
                        <input type="radio" name="sex" value="0" title="男" lay-filter="1" checked>
                        <input type="radio" name="sex" value="1" title="女" lay-filter="2" >
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">qq<span style="color: red;">*</span></label>
                    <div class="layui-input-block">
                        <input type="text" name="qq"  autocomplete="off" placeholder="请输入QQ号码" class="layui-input" required>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">qq昵称<span style="color: red;">*</span></label>
                    <div class="layui-input-block">
                        <input type="text" name="qqName"  autocomplete="off" placeholder="qq昵称" class="layui-input" required>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">qq一键加群|<a href="https://qun.qq.com/join.html" target="_blank">点击获取手机版代码</a></label>
                    <div class="layui-input-block">
                        <textarea name="addQqGroupHtml"  autocomplete="off" placeholder="请输入qq一键加群代码" class="layui-textarea"></textarea>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">手机号码</label>
                    <div class="layui-input-block">
                        <input type="text" name="phonenumber"  autocomplete="off" placeholder="请输入手机号码" class="layui-input" lay-verify="phone">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">用户邮箱</label>
                    <div class="layui-input-block">
                        <input type="text" name="email"  autocomplete="off" placeholder="请输入用户邮箱" class="layui-input" lay-verify="email">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">收款账号</label>
                    <div class="layui-input-block">
                        <input type="text" name="collectionAccount"  autocomplete="off" placeholder="请输入收款账号" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">部门<span style="color: red;">*</span></label>
                    <div class="layui-input-block">
                        <input id="depName" type="text" name="depName"  autocomplete="off" placeholder="请输入部门" class="layui-input" lay-verify="required" required>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">岗位<span style="color: red;">*</span></label>
                    <div class="layui-input-block">
                        <select id="positionId" name="posId">
                            <option th:each="list : ${sysPositionsList}" th:text="${list.postName}" th:value="${list.id}"></option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-block">
                        <input th:each="sysRole:${tsysRoleList}" th:value="${sysRole.id}" th:title="${sysRole.name}" type="checkbox"  lay-skin="primary">
                    </div>
                </div>

            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<th:block th:include="include :: footer"/>
<script>
    layui.use(['form', 'jquery','dtree'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let dtree = layui.dtree;
        let MODULE_PATH = rootPath+"/UserController";
        let DEPAR_PATH=rootPath+"/SysDepartmentController";

        form.render();

        //名称验证
        form.verify({
            username: function(value){
                if(value.length < 4){
                    return '用户账号必须大于4个字符';
                }
            },
            password: function(value){
                let pwd = $("input[name='newpassword']").val();
                let confirm = $("input[name='password']").val();
                if(pwd !== confirm){
                    return '两次密码输入不一致';
                }
            }
        });

        //点击部门
        $("#depName").focus(function () {
            layer.open({
                type: 2,
                title: '部门列表',
                shade: 0.1,
                area: ['320px', '600px'],
                content: DEPAR_PATH + '/deptTree'
            });
        });

        //保存
        form.on('submit(user-save)', function (data) {
            let roleIds = "";
            $('input[type=checkbox]:checked').each(function () {
                roleIds += $(this).val() + ",";
            });
            roleIds = roleIds.substr(0, roleIds.length - 1);
            data.field.roleIds = roleIds;

            $.ajax({
                url: MODULE_PATH+'/add',
                data: data.field,
                dataType: 'json',
                contentType: 'application/x-www-form-urlencoded',
                type: 'post',
                success: function (result) {
                    if (result.code==200) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("user-table");
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });
    })
</script>
</body>
</html>